<?php
/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
namespace app\payment\controller;
use think\facade\Request;
use think\Controller;
use think\Db;

class Pays extends Controller{
    /*
     * 微信-扫码支付-Demo
     * /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     */
    public function index_scan(){
        $wxconfig = config('payconfig.wechatpay');
        $driver = new \pays\PayApi($wxconfig);
        $order = array(
            "ordersn"=>date('YmdHis',time()).  mt_rand(100, 999),
            "total"=>"0.01",
            "subject"=>"订单支付描述",
            "notify"=>"https://xxxxxx/pays/wechat_notify",
        );
        $pay_return = $driver->gateway("wechatpay")->qrcode($order);
        $return = array("code"=>200,"msg"=>"sign ok","data"=>$pay_return);
        return json($return);
    }
    /*
     * 微信-小程序支付-Demo
     */
    public function index_wechat(){
        $wxconfig = config('payconfig.wechatpay');
        $driver = new \pays\PayApi($wxconfig);
        $openId = "ojQ0Z0ZyNpIrHIbfynzXYVSnO4qs";//Openid
        $order = array(
            "openid"=>$openId,
            "ordersn"=>date('YmdHis',time()).  mt_rand(100, 999),
            "total"=>"0.05",
            "subject"=>"订单支付描述",
            "notify"=>"https://xxxxxx/pays/wechat_notify",
        );
        $pay_return = $driver->gateway("wechatpay")->miniapp($order);
        $return = array("code"=>200,"msg"=>"sign ok","data"=>$pay_return);
        return json($return);
    }
	
	/*
     * 微信-APP支付-Demo
     */
    public function index_wechat_app(){
        $wxconfig = config('payconfig.wechatpay');
        $driver = new \pays\PayApi($wxconfig);
        $order = array(
            "ordersn"=>date('YmdHis',time()).  mt_rand(100, 999),
            "total"=>"0.05",
            "subject"=>"订单支付描述",
            "notify"=>"https://xxxxxx/pays/wechat_notify",
        );
        $pay_return = $driver->gateway("wechatpay")->app($order);
        $return = array("code"=>200,"msg"=>"sign ok","data"=>$pay_return);
        return json($return);
    }
	
    //微信支付--异步回调...
    public function wechat_notify(){
        $notify_data = file_get_contents("php://input");
    	if(!$notify_data){
            exit('data error.');
    	}
    	if(!$notify_data){
            return false;
        }
    	$doc = new \DOMDocument();
        $doc->loadXML($notify_data);
        $out_trade_no = $doc->getElementsByTagName("out_trade_no")->item(0)->nodeValue;
        $input["out_trade_no"] = $out_trade_no;
        $wxconfig = config("payconfig.wechatpay");
    	$driver = new \pays\PayApi($wxconfig);
        $query = $driver->gateway("wechatpay")->query($input);
    	if($query["return_code"] == "SUCCESS" && $query["result_code"] == "SUCCESS" && $query["trade_state"] == "SUCCESS"){
    	    //支付异步回调-业务逻辑
    	    //处理业务逻辑
            //file_put_contents("Pays_SDK-Notify.json","Pays-Notify".json_encode($query).PHP_EOL, FILE_APPEND);
    	    //不要删除
    	    exit("SUCCESS");
    	}else{
    	    exit("FAIL");   
    	}
    }
    //微信退款
    public function wechat_refund(){
        $wxconfig = config("payconfig.wechatpay");
        $input = array(
            "order_sn"=>"20181207160841201",//商城订单号
            "total_fee"=>"0.05",//订单实际支付金额
            "refund_fee"=>"0.05",//需要退款金额
        );
    	$driver = new \pays\PayApi($wxconfig);
        $refund = $driver->gateway("wechatpay")->refund($input);
        if($refund["return_code"] == "SUCCESS" && $refund["result_code"] == "SUCCESS"){
    	    exit("退款-SUCCESS");
    	}else{
    	    exit("退款-FAIL");   
    	}
    }
	//微信商户转账到个人
	public function transfers(){
        $wxconfig = config("payconfig.wechatpay");//微信
        $input = array(
            "openid"=>"OPENID",//收款用户OPENID
            "order_sn"=>"20190509751791".rand(1,9),//商户订单号，需保持唯一性
            "total_fee"=>0.3,//转账金额，最低0.3
        );
		$driver = new \pays\PayApi($wxconfig);
        $transfers = $driver->gateway("wechatpay")->transfers($input);
		
        if($refund["return_code"] == "SUCCESS" && $refund["result_code"] == "SUCCESS"){
    	    exit("转账-SUCCESS");
    	}else{
    	    exit("转账-FAIL");   
    	}
		
    }
	
    /******微信支付---Demo-End*****/
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    /*
     * 随行付支付-Demo
     */
    public function index_sxf(){
        $sxfconfig = config('payconfig.sxfpay');
        $driver = new \pays\PayApi($sxfconfig);
        $order = array(
            "mno"=>"836513653110229",
            "ordersn"=>date('YmdHis',time()).  mt_rand(100, 999),
            "total"=>"20",
            "subject"=>"订单支付描述",
            "notify"=>"http://xxxxxx/pays/sxf_notify"
        );
        $pay_return = $driver->gateway("sxfpay")->qrcode($order);
        $return = array("code"=>200,"msg"=>"return ok","data"=>$pay_return);
        return json($return);
    }
    public function index_sxf_jsapi(){
        $sxfconfig = config('payconfig.sxfpay');
        $driver = new \pays\PayApi($sxfconfig);
        
        $appid = '836135153110106';
        $appsecret = 'wx3d6ee488e*********';

        $order = array(
            "appid"=>$appid,
            "openid"=>$openId,
            "appsecret"=>$appsecret,
            "ordersn"=>$orderinfo["sn"].rand(1,99),
            "total"=>$total_fee*100,
            "subject"=>"订单支付描述",
            "notify"=>"http://xxxxxxxx.com/api/payment/weixin_notify_web"
        );
        
        $pay_return = $driver->gateway("sxfpay")->miniapp($order);
        if($pay_return['code'] == 200){
            $return = array("code"=>1,"msg"=>"return ok","data"=>$pay_return['data']);
        }else{
            $return = array("code"=>0,"msg"=>"return no","data"=>$pay_return['data']);
        }
        return json($return);
    }
    public function sxf_notify(){
        //异步回调...
        $notify_data = file_get_contents("php://input");
    	if(!$notify_data){
            exit('');
    	}
    	file_put_contents("Pays_SDK-sxfpay.json","Pays_SDK:Sxf".json_encode($notify_data).PHP_EOL, FILE_APPEND);
    }
    /*******随行付支付--Demo-ENd*************/
    
    
    /*
     * 支付宝支付-Demo
     */
    //二维码支付
    public function index_alipay(){
        $alipayconfig = config('payconfig.alipay');
        $driver = new \pays\PayApi($alipayconfig);
        $order = array(
            "ordersn"=>date('YmdHis',time()).  mt_rand(100, 999),
            "total"=>"0.01",
            "subject"=>"订单支付描述",
            "notify_url"=>"https://xxxxxx/pays/alipay_notify",
            "return_url"=>"https://xxxxxx/pays/alipay_return_url",
        );
        $pay_return = $driver->gateway("alipay")->qrcode($order);
        $return = array("code"=>200,"msg"=>$pay_return["msg"],"data"=>$pay_return["data"]);
        return json($return);
    }
    //手机支付
    public function index_alipay_h5(){
        $alipayconfig = config('payconfig.alipay');
        $driver = new \pays\PayApi($alipayconfig);
        $order = array(
            "ordersn"=>date('YmdHis',time()).  mt_rand(100, 999),
            "total"=>"0.01",
            "subject"=>"订单支付描述",
            "notify_url"=>"https://xxxxxx/pays/alipay_notify",
            "return_url"=>"https://xxxxxx/pays/alipay_return_url",
        );
        $pay_return = $driver->gateway("alipay")->h5($order);
    }
    public function alipay_return_url(){
        echo "支付成功";
        $post_data = $_POST;
        $get_data = $_GET;
        $return_data = array_merge($post_data,$get_data);
    	file_put_contents("Pays_SDK-alipay.json","Pays_SDK:Alipay".json_encode($return_data).PHP_EOL, FILE_APPEND);
    }
    public function alipay_notify(){
        //异步回调...
        $notify_data = $_POST;
    	file_put_contents("Pays_SDK-alipay.json","Pays_SDK:Alipay".json_encode($notify_data).PHP_EOL, FILE_APPEND);
        
        $ordersn = $_POST["out_trade_no"];
        $alipayconfig = config("payconfig.alipay");
        $input = array(
            "order_sn"=>$ordersn,//商城订单号
        );
    	$driver = new \pays\PayApi($alipayconfig);
        $refund = $driver->gateway("alipay")->query($input);
        dump($refund);
        
    }
    //支付宝--查询
    public function alipay_query(){
        $alipayconfig = config("payconfig.alipay");
        $input = array(
            "order_sn"=>"20181207160841201",//商城订单号
        );
    	$driver = new \pays\PayApi($alipayconfig);
        $refund = $driver->gateway("alipay")->query($input);
        dump($refund);
    }
    //支付宝退款
    public function alipay_refund(){
        $alipayconfig = config("payconfig.alipay");
        $input = array(
            "order_sn"=>"20181207160841201",//商城订单号
            "total_fee"=>"0.05",//订单实际支付金额
            "refund_reason"=>"支付宝退款",//退款原因
        );
    	$driver = new \pays\PayApi($alipayconfig);
        $refund = $driver->gateway("alipay")->refund($input);
        dump($refund);
        die;
        /*
        if($refund["return_code"] == "SUCCESS" && $refund["result_code"] == "SUCCESS"){
    	    exit("退款-SUCCESS");
    	}else{
    	    exit("退款-FAIL");   
    	}
         */
    }
	
	/* 个人转账收款*/
    public function transfertest(){
        $alipayconfig = config("payconfig.alipay");
		$input = array(
			'total' => 10, //转账金额
			'ordersn'=>date("YmdHis").rand(100,999),
			'account'=>"123@qq.com",//支付宝收款账户
		);
        $driver = new \pays\PayApi($alipayconfig);
        $getway = $driver->gateway("alipay");
		$res = $getway->transfer($input);
		dump($res);
    }
    /*******支付宝支付--Demo-ENd*************/
}
